﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace daStaticAnalyser.Analysers
{
    using daStaticAnalyser;
    using daBase.Objects;
    using daBase.Analyser;
    using daSyntaxAnalyser;
    using daSyntaxAnalyser.SyntaxTree;

    [AnalyserDefaultSettings("Метрика обращения к глобальным переменным", "test", 0, "test")]
    class GlobalValRefAnalyzer : BaseStaticAnalyser
    {
        private int GetData(ISyntaxTreeElement Element)
        {
            if (Element == null)
                return 0;
            int result = 0;
            if (Element is StandartVariableExpressionElement)
            {
                String temp = Element.ToString();
                if (!temp.Contains("("))
                {
                    String[] strs = temp.Split('.');
                    if (GlobalVar.Contains(strs[0])) result++;
                }
            }

            foreach (ISyntaxTreeElement element in Element)
            {
                result += GetData(element);
            }
            return result;
        }

        protected override void DoAnalyse(SyntaxTreeArgs e)
        {
            String MethodName = "";
            if (CheckMethod_GetName(e, out MethodName))
            {
                int Aup = GetData((e.Element as OuterMethodElement).Body);
                int Pup = GlobalVar.Count;
                float Rup = ((float)Aup) / Pup;
                AddMetric("Метрика обращения к глобальным переменным",
                            Rup,
                            MethodName,
                            e.Element.StartInfo.File,
                            e.Element.StartInfo.Line);
            }
            else
            {
                GlobalVarFind(e.Element);
            }
        }

        protected override void DoFinishAnalyse(EventArgs e)
        {
            GlobalVar.Clear();
        }
    }
}
